<template>
  <el-container style="height: 100vh">
    <!--    头部-->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" style="height: 50px;width: 50px;
        vertical-align: center">
        <span>电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!--      侧边栏-->
      <el-aside :width="isCollapse ?'64px':'200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
                :default-active="$route.path"
                background-color="#313743"
                text-color="#fff"
                active-text-color="#409eff"
                unique-opened
                router
                :collapse="isCollapse"
                :collapse-transition="false">
          <!--          一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menusList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--            二级菜单-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children"
                          :key="subItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--      右侧主体-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    name: "Home",
    created() {
      this.getMenusList()
    },
    data() {
      return {
        // 菜单列表
        menusList: [],
        // 图标对象
        iconObj: {
          '125': 'iconfont icon-user',
          '103': 'iconfont icon-tijikongjian',
          '101': 'iconfont icon-shangpin',
          '102': 'iconfont icon-danju',
          '145': 'iconfont icon-baobiao'
        },
        // 是否折叠
        isCollapse: false
      }
    },
    methods: {
      logout() {
        window.sessionStorage.clear()
        this.$router.push('/login')
      },
      async getMenusList() {
        const {data: res} = await this.$http('menus')
        if (res.meta.status !== 200)
          return this.$message.error(res.meta.msg)
        this.menusList = res.data
      },
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-header {
    background-color: #373D41;
    display: flex;
    padding-left: 1px;
    justify-content: space-between;
    align-items: center;
    color: white;
    font-size: 24px;
    font-family: Arial, serif;

    > div {
      display: flex;
      align-items: center;

      span {
        margin-left: 15px;
      }
    }
  }

  .el-aside {
    background-color: #313743;
    transition: all 0.3s;

    .toggle-button {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      text-align: center;
      color: white;
      letter-spacing: 0.3em;
      cursor: pointer;
    }

    .el-menu {
      border-right: none;

    }
  }

  .el-main {
    background-color: #e9edf1;
  }

  .iconfont {
    margin-right: 10px;
  }
</style>
